package com.raitcon.hibernate.db;

import com.raitcon.hibernate.bean.Asignatura;
import com.raitcon.hibernate.bean.Departamento;
import com.raitcon.hibernate.bean.HibernateSessionFactory;
import com.raitcon.hibernate.bean.Profesor;
import static com.raitcon.hibernate.db.DepartamentoDB.log;
import com.raitcon.hibernate.factory.HibernateUtil;
import java.util.List;
import org.apache.log4j.Logger;
import org.hibernate.HibernateException;
import org.hibernate.Query;
import org.hibernate.Session;
import org.hibernate.Transaction;

public class AsignaturaDB {

    protected static Logger log = Logger.getLogger(ProfesorDB.class);
    private Session session = null;

    public AsignaturaDB() {
        this.session = HibernateUtil.getSessionFactory().getCurrentSession();
    }

    public void insertAsignatura(Asignatura asi) throws HibernateException, Exception {
        try {
            System.out.println("metodo insert");
            session = HibernateSessionFactory.getSession();
            System.out.println("session: " + session);
            System.out.println("Try");
            Transaction ts = session.beginTransaction();
            System.out.println("Transaction");
            session.save(asi);
            System.out.println("Save");
            ts.commit();
            System.out.println("Se inserto correctamente");
        } catch (HibernateException ex1) {
            System.out.println("HibernateException : " + ex1);
        } catch (Exception ex2) {
            System.out.println("Exception : " + ex2);
        } finally {
            session.close();
        }
    }

    public void updateAsignatura(Asignatura asi, long id) throws HibernateException, Exception {

        try {
            System.out.println("metodo update");
            session = HibernateSessionFactory.getSession();
            System.out.println("try");
            Transaction ts = session.beginTransaction();
            System.out.println("Transaction");
            Asignatura asiUpdate = (Asignatura) session.get(Asignatura.class, id);
            session.update(asiUpdate);
            System.out.println("Update");
            ts.commit();
            System.out.println("Se modifico correctamente");
        } catch (HibernateException ex1) {
            throw ex1;
        } catch (Exception ex2) {
            throw ex2;
        } finally {
            session.close();
        }

    }

    public void deleteAsignatura(Asignatura asi) throws HibernateException, Exception {
        session = HibernateSessionFactory.getSession();
        try {
            Transaction ts = session.beginTransaction();
            session.delete(asi);
            ts.commit();
            System.out.println("Se elimno correctamente");
        } catch (HibernateException ex1) {
            throw ex1;
        } catch (Exception ex2) {
            throw ex2;
        } finally {
            session.close();
        }

    }

    public List<Asignatura> getAsignaturaAll() {
        List<Asignatura> asignaturaList = null;
        try {
            System.out.println("metodo getAsignaturaAll");
            session = HibernateSessionFactory.getSession();
            Transaction ts = session.beginTransaction();
            Query q = session.createQuery("from Asignatura as asignatura order by id_area");
            asignaturaList = (List<Asignatura>) q.list();
        } catch (Exception e) {
            e.printStackTrace();
        }
        return asignaturaList;
    }

    public List getAsignaturaById(String codAsi) {

        List<Asignatura> asiList = null;
        try {
            Transaction tx = session.beginTransaction();
            Query q = session.createQuery("from asignatura WHERE id_asignatura ='" + codAsi + "'");
            System.out.println("Query: " + q.toString());
            asiList = (List<Asignatura>) q.list();
        } catch (Exception e) {
            log.debug("Error:" + e.getMessage());
            e.printStackTrace();
        }
        return asiList;
    }
    
    
    public List getAsignaturaByIdArea(String codArea) {
       
        List<Asignatura> asignaturaList = null;
        try {
            System.out.println("CodArea:"+codArea);
            org.hibernate.Transaction tx = session.beginTransaction();
            Query q = session.createQuery ("from Asignatura as asignatura WHERE id_area =" +codArea);
            asignaturaList = (List<Asignatura>) q.list();
        } catch (Exception e) {
            System.out.println("Error Area:"+codArea);
            log.debug("Error:"+e.getMessage() );
            e.printStackTrace();
        }
        return asignaturaList;
      }
    
    
     public Integer asignaturaById(Integer id) {
       
       try {
            System.out.println("idAsignatura:"+id);
            Asignatura asignatura=new Asignatura();
            org.hibernate.Transaction tx = session.beginTransaction();
            Query q = session.createQuery ("from Asignatura as asignatura WHERE id_asignatura=" +id);
           asignatura= (Asignatura) q.uniqueResult();
           return asignatura.getTipoAsignatura();
        } catch (Exception e) {
            System.out.println("Error obtener tipo asignatura:"+id);
            log.debug("Error obtener tipo asignatura:"+e.getMessage() );
            e.printStackTrace();
            return 0;
        
       }
        
      }
    
}
